package com.example.demo.level2;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zhangyang
 * @Date: 2020/4/19 15:22
 */
@SuppressWarnings("JavaDoc")
class Person {
    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

public class LearnStreamSimple {

    @SuppressWarnings({"RedundantStringFormatCall", "Convert2Diamond"})
    @Test
    public void aboutStream() {
        List<Person> persons = new ArrayList<Person>();
        Person p1 = new Person();
        p1.setAge(21);
        persons.add(p1);
        Person p2 = new Person();
        p2.setAge(19);
        persons.add(p2);
        List<Integer> ages = persons
                // 转换成stream
                .stream()
                // 筛选出年龄大于20的
                .filter(p -> p.getAge() > 20)
                // 映射出年龄 这样的写法等同于 p->p.getAge() 感兴趣的可以试一下
                .map(Person::getAge)
                // 收集结果，转成List
                .collect(Collectors.toList());
        ages.forEach(a -> System.out.println(String.format("Age-%s", a)));
    }
}
